package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.common.result.Result;
import com.system.runtai.entity.*;
import com.system.runtai.service.EmergencyVehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 应急车辆 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-04-05
 */
@RestController
@Api(tags = "应急车辆")
@RequestMapping("/runtai/emergency-vehicle")
public class EmergencyVehicleController {

    @Autowired
    private EmergencyVehicleService emergencyVehicleService;

    @ApiOperation(value = "所有应急车辆")
    @GetMapping("/emergencyVehicleList")
    public Result<List<EmergencyVehicle>> emergencyVehicleList(){
        List<EmergencyVehicle> list = emergencyVehicleService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "应急车辆详情")
    @GetMapping("/emergencyVehicle/{id}")
    public Result<EmergencyVehicle> emergencyVehicleById(@PathVariable Integer id){
        EmergencyVehicle byId = emergencyVehicleService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation(value = "添加应急车辆")
    @PostMapping("/saveEmergencyVehicle")
    public Result<EmergencyVehicle> saveEmergencyVehicle(@RequestBody EmergencyVehicle emergencyVehicle){

        emergencyVehicle.setCreateTime(LocalDateTime.now());
        boolean save = emergencyVehicleService.save(emergencyVehicle);

        if (save){
            return Result.ok(emergencyVehicle).message("添加成功");
        }else {
            return Result.fail(emergencyVehicle).message("添加失败");
        }
    }

    @ApiOperation(value = "删除应急车辆")
    @PostMapping("delectEmergencyVehicle/{id}")
    public Result delectEmergencyVehicle(@PathVariable Integer id){

        boolean b = emergencyVehicleService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "修改应急车辆")
    @PostMapping("/updateEmergencyVehicle")
    public Result<EmergencyVehicle> updateEmergencyVehicle(@RequestBody EmergencyVehicle emergencyVehicle){

        emergencyVehicle.setUpdateTime(LocalDateTime.now());
        boolean update = emergencyVehicleService.updateById(emergencyVehicle);

        if (update){
            return Result.ok(emergencyVehicle).message("修改成功");
        }else {
            return Result.fail(emergencyVehicle).message("修改失败");
        }
    }

    @ApiOperation(value = "分页查询应急车辆")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<EmergencyVehicle>> index13(@PathVariable Long page, @PathVariable Long limit, EmergencyVehicle emergencyVehicle){

        Page<EmergencyVehicle> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<EmergencyVehicle> wrapper = new LambdaQueryWrapper<>();

        Integer id = emergencyVehicle.getId();

        if (id!=null){
            wrapper.eq(EmergencyVehicle::getId,id);
        }

        IPage<EmergencyVehicle> iPage = emergencyVehicleService.page(recommendPage,wrapper);

        return Result.ok(iPage);
    }

    @ApiOperation(value = "应急车辆名称筛选")
    @GetMapping("/filkte")
    public Result<IPage<EmergencyVehicle>> filtemergencyVehicleName(@RequestParam(required = false) String carName,@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit){

        List<EmergencyVehicle> list = null;
        if (carName!=null) {
        LambdaQueryWrapper<EmergencyVehicle> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(EmergencyVehicle::getCarname,carName);
        list = emergencyVehicleService.list(wrapper);

//            PageHelper.startPage(page,limit);
//            PageInfo pageInfo = new PageInfo(list);
//
//            return Result.ok(pageInfo);

            Page<EmergencyVehicle> recommendPage = new Page<>(page,limit);
            IPage<EmergencyVehicle> iPage = emergencyVehicleService.page(recommendPage,wrapper);
            return Result.ok(iPage);
        }else if (carName==null){
            list = emergencyVehicleService.list();

            Page<EmergencyVehicle> recommendPage = new Page<>(page,limit);
            IPage<EmergencyVehicle> iPage = emergencyVehicleService.page(recommendPage);
            return Result.ok(iPage);
        }
        return Result.fail();
    }

}

